package com.ncmmall.application.business.goods;

import cn.hutool.core.lang.Assert;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.goods.channel.GoodsChannel;
import com.ncmmall.domain.business.goods.channel.GoodsChannelRepository;
import com.ncmmall.domain.business.goods.channel.QGoodsChannel;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * @author linxs
 * @Description: 商品类目业务处理
 * @Date: 2019-06-28 16:13
 */
@Service
@Transactional
public class GoodsChannelApplication {

    @Autowired
    private GoodsChannelRepository channelRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 添加子类目
     */
    public void createChildrenChannel(GoodsChannel channel) {
        GoodsChannel parentChannel = queryChannelService.findOne(QGoodsChannel.goodsChannel.id.eq(channel.getParent().getId()), GoodsChannel.class);
        channel.setLevel(parentChannel.getLevel() + 1);
        channel.setCreateTime(new Timestamp(System.currentTimeMillis()));

        int count = queryChannelService.selectCount("select count(*) from mall_goods_channel where level = :level and name = :name and deleteStatus = 0",
                ImmutableMap.of("level", channel.getLevel(), "name", channel.getName()));
        Assert.isTrue(count == 0, "类目名称已存在");

        if (channel.getLevel() == 3) {
            Requires.isTrue(StringUtils.isNotBlank(channel.getIcon()), "分类图片不能为空");
        }

        channelRepository.save(channel);
    }


    /**
     * 更新类目信息
     */
    public void modify(GoodsChannel params) {

        GoodsChannel channel = queryChannelService.findOne(QGoodsChannel.goodsChannel.id.eq(params.getId()), GoodsChannel.class);

        if (!params.getName().equals(channel.getName())) {
            int count = queryChannelService.selectCount("select count(*) from mall_goods_channel where name = :name and level = :level and deleteStatus = 0", ImmutableMap.of("name", params.getName(), "level", channel.getLevel()));
            Assert.isTrue(count == 0, "类目名称已存在");
        }

        if (channel.getLevel() == 3) {
            Requires.isTrue(StringUtils.isNotBlank(params.getIcon()), "分类图片不能为空");
        }

        channel.setName(params.getName());
        channel.setIcon(params.getIcon());
        channel.setStatus(params.getStatus());
        channel.setRecommend(params.isRecommend());
        channel.setSupplierRecommend(params.isSupplierRecommend());
        channel.setParent(params.getParent());
        channelRepository.save(channel);

        for (GoodsChannel chilrenChannel : channel.getChildren()) {
            chilrenChannel.setStatus(params.getStatus());
            channelRepository.save(chilrenChannel);

            for (GoodsChannel bottonChannel : chilrenChannel.getChildren()) {
                bottonChannel.setStatus(params.getStatus());
                channelRepository.save(bottonChannel);
            }

        }

    }

    /**
     * 逻辑删除
     *
     * @param channel
     */
    public void remove(GoodsChannel channel) {

        channel.setDeleteStatus(1);
        channelRepository.save(channel);
    }

    /**
     * 添加顶级类目
     *
     * @param channel
     */
    public void createTopChannel(GoodsChannel channel) {
        channel.setCreateTime(new Timestamp(System.currentTimeMillis()));
        channel.setLevel(1);
        channelRepository.save(channel);
    }
}
